package cn.flying.cloud.flow.web;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.utils.lang.RandomUtil;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.flow.core.service.ProcessManageService;
import cn.flying.cloud.flow.model.FlowStartVo;

/**
 * @author: admin
 * @date: 2025年05月12日 20:01
 * @version: 1.0
 */
@RestController
@RequestMapping("/flow/demo")
public class DemoController {

    @Resource
    private ProcessManageService processManageService;

    @RequestMapping("start")
    public Rt<?> start(@RequestBody FlowStartVo startVo) {
        startVo.setBizKey(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.getRandomNumber(4));
        String procInstId = processManageService.startProcess(startVo);
        Map<String, String> map = new HashMap<>();
        map.put("bizKey", startVo.getBizKey());
        map.put("procInstId", procInstId);
        return Rt.success(map);
    }

    @RequestMapping("start2")
    public Rt<?> start2(String bizKey, String processDefKey, Map<String, Object> variables, String userId, String tenantId) {
        if (Ft.String.isBlank(bizKey)) {
            bizKey = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.getRandomNumber(4);
        }
        String procInstId = processManageService.startProcess(bizKey, processDefKey, variables, userId, tenantId);
        Map<String, String> map = new HashMap<>();
        map.put("bizKey", bizKey);
        map.put("procInstId", procInstId);
        return Rt.success(map);
    }
}
